package com.jxc.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jxc.dao.BaseDaoI;
import com.jxc.model.po.Deliverlist;
import com.jxc.model.po.Goods;
import com.jxc.model.po.Transferlist;
import com.jxc.model.po.DeliverlistItem;
import com.jxc.model.vo.DataGrid;
import com.jxc.model.vo.VDeliverlistItem;
import com.jxc.service.DeliverlistItemServiceI;
import com.jxc.service.TransferlistItemServiceI;

@Service("deliverlistItemService")
public class DeliverlistItemServiceImpl implements DeliverlistItemServiceI {

	@Autowired
	private BaseDaoI<DeliverlistItem> deliverlistItemDao;

	@Override
	public List<VDeliverlistItem> combobox(VDeliverlistItem vtli) {
		return this.changeModel(this.find(vtli));
	}

	@Override
	public DataGrid datagrid(VDeliverlistItem vtli) {
		DataGrid dg = new DataGrid();
		dg.setRows(this.changeModel(this.find(vtli)));
		dg.setTotal(this.total(vtli));
		return dg;
	}

	@Override
	public void delete(String ids) {
		if (ids != null) {
			for (String id : ids.split(",")) {
				if (!id.trim().equals("0")) {
					DeliverlistItem tl = deliverlistItemDao.get(DeliverlistItem.class, Long.parseLong(id.trim()));
					if (tl != null) {
						deliverlistItemDao.delete(tl);
					}
				}
			}
		}
	}

	@Override
	public void save(VDeliverlistItem vtli) {
		DeliverlistItem tli = new DeliverlistItem();
		BeanUtils.copyProperties(vtli, tli);
		if (vtli.getGid() != null) {
			tli.setGoods(new Goods(vtli.getGid()));
		}
		if (vtli.getDlid() != null) {
			tli.setDeliverlist(new Deliverlist(vtli.getDlid()));
		}
		this.deliverlistItemDao.save(tli);
	}

	@Override
	public void update(VDeliverlistItem vdli) {
		DeliverlistItem tli = deliverlistItemDao.get(DeliverlistItem.class, vdli.getId());
		BeanUtils.copyProperties(vdli, tli, new String[] { "id" });
		if (vdli.getGid() != null) {
			tli.setGoods(new Goods(vdli.getGid()));
		}
		if (vdli.getDlid() != null) {
			tli.setDeliverlist(new Deliverlist(vdli.getDlid()));
		}
		this.deliverlistItemDao.update(tli);
	}

	private List<VDeliverlistItem> changeModel(List<DeliverlistItem> dlis) {
		List<VDeliverlistItem> vdlis = new ArrayList<VDeliverlistItem>();
		if (dlis != null && dlis.size() > 0) {
			for (DeliverlistItem dli : dlis) {
				VDeliverlistItem vdli = new VDeliverlistItem();
				BeanUtils.copyProperties(dli, vdli);
				if (dli.getGoods() != null) {
					Goods goods = dli.getGoods();
					vdli.setGid(goods.getId());
					if(goods.getId()!=null){
						vdli.setGid(goods.getId());
					}
					if(goods.getName()!=null){
						vdli.setGname(goods.getName());
					}
					if(goods.getBarcode()!=null){
						vdli.setGbarcode(goods.getBarcode());
					}
					if(goods.getBrand()!=null&&goods.getBrand().getName()!=null){
						vdli.setGbrand(goods.getBrand().getName());
					}
					if(goods.getType()!=null){
						vdli.setGtype(goods.getType());
					}
					if(goods.getColor()!=null){
						vdli.setGcolor(goods.getColor());
					}
					if(goods.getSize()!=null){
						vdli.setGsize(goods.getSize());
					}
					
				}
				if (dli.getDeliverlist() != null) {
					Deliverlist dl = dli.getDeliverlist();
					vdli.setDlid(dl.getId());
				}
				if (dli.getDealerGoods() != null) {
					Goods dealerGoods = dli.getDealerGoods();
					vdli.setDealerGoodsId(dealerGoods.getId());
					if(dealerGoods.getName()!=null){
						vdli.setDealerGoodsName(dealerGoods.getName());
					}
					if(dealerGoods.getBarcode()!=null){
						vdli.setDealerGoodsbarcode(dealerGoods.getBarcode());
					}
					if(dealerGoods.getBrand()!=null&&dealerGoods.getBrand().getName()!=null){
						vdli.setDealerGoodsbrand(dealerGoods.getBrand().getName());
					}
					if(dealerGoods.getType()!=null){
						vdli.setDealerGoodstype(dealerGoods.getType());
					}
					if(dealerGoods.getColor()!=null){
						vdli.setDealerGoodscolor(dealerGoods.getColor());
					}
					if(dealerGoods.getSize()!=null){
						vdli.setDealerGoodssize(dealerGoods.getSize());
					}
				}
				vdlis.add(vdli);
			}
		}
		return vdlis;
	}

	private List<DeliverlistItem> find(VDeliverlistItem vtli) {
		String hql = "from DeliverlistItem t where 1=1 ";

		List<Object> values = new ArrayList<Object>();
		hql = addWhere(vtli, hql, values);

		if (vtli.getSort() != null && vtli.getOrder() != null) {
			hql += " order by  " + vtli.getSort() + " " + vtli.getOrder();
		}
		return deliverlistItemDao.find(hql, values, vtli.getPage(), vtli.getRows());
	}

	private Long total(VDeliverlistItem vtli) {
		String hql = "select count(*) from DeliverlistItem t where 1=1 ";
		List<Object> values = new ArrayList<Object>();
		hql = addWhere(vtli, hql, values);
		return deliverlistItemDao.count(hql, values);
	}

	private String addWhere(VDeliverlistItem vtli, String hql, List<Object> values) {
		if (vtli.getGid() != null) {
			hql += " and t.goods.id = ? ";
			values.add(vtli.getGid());
		}

		if (vtli.getDlid() != null) {
			hql += " and t.deliverlist.id = ? ";
			values.add(vtli.getDlid());
		}

		return hql;
	}

	@Override
	public void batchSave(List<VDeliverlistItem> vtlis, Long tlid) {
		for (VDeliverlistItem vtli : vtlis) {
			vtli.setDlid(tlid);
			this.save(vtli);
		}
	}

}
